package com.kuang.demo.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.kuang.demo.bean.BaseRespVo;
import com.kuang.demo.bean.TemporaryLoginInfo;
import com.kuang.demo.bo.zzy.IPUtil;
import com.kuang.demo.bo.zzy.IntegerTest;
import com.kuang.demo.bo.zzy.LoginInfo;
import com.kuang.demo.bo.zzywx.auth.UsernamePwd;
import com.kuang.demo.config.wx.MyToken;
import com.kuang.demo.mapper.LoginInfoMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/auth")
public class AuthController {
    @Autowired
    LoginInfoMapper loginInfoMapper;

    //登录接口
    @RequestMapping("/login")
    public BaseRespVo LoginControl(@RequestBody UsernamePwd usernamePwd){

        //TODO:捕获异常可以优化
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(new MyToken(usernamePwd.getUsername(),usernamePwd.getPassword(),"admin"));
            String principal = (String) subject.getPrincipal();
            String id = (String) subject.getSession().getId();


//            //操作日志
//            loginInfoMapper.addLog()

            //获取当前请求对象
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String ip = IPUtil.getIp(request);
            loginInfoMapper.insertIntoLog(principal,ip);



            return BaseRespVo.ok(id);
        }catch (Exception e){
            System.out.println("权限或者密码不对");
        }
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(605);
        baseRespVo.setErrmsg("用户密码不对或没有权限");

        return baseRespVo;
    }

    //登出接口
    @RequestMapping("/logout")
    public BaseRespVo LoginOut(){
        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();


        //获取当前请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String ip = IPUtil.getIp(request);
        loginInfoMapper.insertIntoLog2(principal,ip);
        subject.logout();

        return BaseRespVo.ok();
    }

//    @RequestMapping("/info")
//    public BaseRespVo info(String token){
//        Subject subject = SecurityUtils.getSubject();
//        String principal = (String) subject.getPrincipal();
//
//        IntegerTest integerTest = new IntegerTest();
//        IntegerTest integers = loginInfoMapper.selectRoles(principal);
//
//        Integer[] roles = integers.getRoles();
//
//        LoginInfo loginInfo = new LoginInfo();
//        List<String> roleList = new ArrayList<>();
//
//        //拿着角色id去取名字
//        for (Integer role : roles) {
//            roleList.add(loginInfoMapper.selectRoleById(role));
//        }
//        loginInfo.setRoles(roleList);
//        loginInfo.setName(principal);
//
//        //avatar
//        String avatar = loginInfoMapper.selectAvatarByUsername(principal);
//        loginInfo.setAvatar(avatar);
//
//        List<String> arrayList = new ArrayList();
//        //perms
//        for (Integer role : roles) {
//            if(role == 1){
//                List<String> strings = new ArrayList<>();
//                strings.add("*");
//                loginInfo.setPerms(strings);
//            }
//        }
//        if(loginInfo.getPerms() == null) {
//            for (Integer role : roles) {
//                //拿着roleId去取permissions
//                List<String> permissions = loginInfoMapper.selectPermissionsByRoleId(role);
//
//                for (String permission : permissions) {
//                    arrayList.add(permission);
//                }
//            }
//            loginInfo.setPerms(arrayList);
//        }
//
//        return BaseRespVo.ok(loginInfo);
//
//    }

    @RequestMapping("/info")
    public BaseRespVo authInfo(){
    TemporaryLoginInfo temporaryLoginInfo=new TemporaryLoginInfo();
    List<String> roles=new ArrayList<>();
    List<String> perms=new ArrayList<>();
    roles.add("超级管理员");
    perms.add("*");
    temporaryLoginInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    temporaryLoginInfo.setName("admin123");
    temporaryLoginInfo.setRoles(roles);
    temporaryLoginInfo.setPerms(perms);
    return BaseRespVo.ok(temporaryLoginInfo);
}
}
